package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //1.获取当前登录用户，为购物车对象赋值
        //2.根据当前登录用户ID 及 本次添加的菜品ID/套餐ID，查询购物车数据是否存在
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        //3.不存在,则添加到购物车,
        if (cart ==null){
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
        }else {
            //4.存在,则在数量上+1
            cart.setNumber(cart.getNumber()+1);
            shoppingCart=cart;
            shoppingCartService.updateById(cart);
        }
        return R.success(shoppingCart);
    }

    /**
     * 查询购物车
     * @param
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //创建查询条件
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.orderByAsc("create_time");
        //调用方法
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * delete  from ShoppingCart where user_id=?
     * @param
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);

        return R.success("删除成功");
    }

    /**
     * 删减商品数量
     * @param shoppingCart
     * @return
     */

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户id
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        //获取数据库里信息
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        //判断number的数量情况
        if (cart.getNumber()==1){
            shoppingCartService.remove(queryWrapper);
        }else {
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.updateById(cart);
        }
        return R.success("修改成功");
    }
}
